#include "mainwindow.h"
#include <QApplication>
#include <QProcess>
#include <QSharedMemory>
#include <qmessagebox.h>

int main(int argc, char *argv[])
{
    QSharedMemory shared("APP");//这个名字无所谓
        if(shared.attach()){
            int app_pid = 0;
            QMessageBox box;
            box.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
            box.setText(QStringLiteral("程序已启动，是否关闭上一次执行程序"));
            if(box.exec() == QMessageBox::Yes){
                QProcess process;
                process.start("tasklist.exe /FI \"IMAGENAME eq app.exe\"");//这里把app换成工程文件.pro里面TARGET的值
                if(process.waitForFinished()){
                QByteArray result = process.readAll();
                int left = result.indexOf("Console",1);
                QByteArray m_temp = result.mid(left-6,5);
                QString m_str = m_temp;
                app_pid = m_str.replace(" ","").toInt();
                }
                QString cmd = QString("taskkill /F /PID %1 /T").arg(app_pid);
                process.execute(cmd);
            }else{
                exit(0);
            }
        }
        shared.create(1);

    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
